const Clutter    = imports.gi.Clutter;
const Gdk        = imports.gi.Gdk;
const GdkPixbuf  = imports.gi.GdkPixbuf;
const GLib       = imports.gi.GLib;
const GtkClutter = imports.gi.GtkClutter;
const Gtk        = imports.gi.Gtk;
const Lang       = imports.lang;
const Mx         = imports.gi.Mx;
const OctopusClutterGtk = imports.gi.OctopusClutterGtk;
const Octopus    = imports.gi.Octopus;
const OctopusMx  = imports.gi.OctopusMx;

const LoadScreen    = imports.loadScreen;
const ControlPanel  = imports.controlPanel;
const MedialibPanel = imports.medialibPanel;
const PlaylistPanel = imports.playlistPanel;
const BottomPanel   = imports.bottomPanel;

function start() {
	Gdk.disable_multidevice ();
	GtkClutter.init (null);
	window.global = Octopus.Global.get ();
	try {
		//requires mx-css file at index 0
		Mx.Style.get_default ().load_from_file (global.css_files_get (0));
	} catch (e) {
		print ("ERROR loading css file: " + e.message);
	}

	//this.applicationWindow = new GtkClutter.Window ();
	//this.applicationWindow = new Gtk.Window({ type: Gtk.WindowType.TOPLEVEL });
	this.applicationWindow = new Gtk.ApplicationWindow ();
	this.applicationWindow.connect ('destroy', Lang.bind(this, function () {
		//FIXME: get_position only returns {0, 0}
		//FIXME: get_size only returns {1024, 768}
		//position and size never get updated
		let position = Octopus.utils_get_position (this.applicationWindow);
		//let size = Octopus.utils_get_size (this.applicationWindow);
		this.config.set_integer("classic", "x", position[0]);
		this.config.set_integer("classic", "y", position[1]);
		//this.config.set_integer("classic", "width", size[0]);
		//this.config.set_integer("classic", "height", size[1]);
		this.config.set_integer("classic", "gravity", this.applicationWindow.gravity);
		this.config.set_integer("classic", "width", this.stage.width);
		this.config.set_integer("classic", "height", this.stage.height);
		this.config.save ();
		Gtk.main_quit ();
	}));
	try {
		let iconfile = GLib.build_filenamev ([global.data_path, "octopus-48.png"]);
		this.applicationWindow.icon =  GdkPixbuf.Pixbuf.new_from_file (iconfile);
	} catch (e) {
		print ("ERROR loading application icon: " + e.message);
	}
	let provider = new Gtk.CssProvider ();
	let display = Gdk.Display.get_default ();
	let screen = display.get_default_screen ();
	let style = this.applicationWindow.get_style_context ();
	style.add_provider_for_screen (screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
	try {
		//requires gtk-css file at index 1
		provider.load_from_path (global.css_files_get (1));
	} catch (e) {
		print ("ERROR loading css file: " + e.message);
	}

	this.eventManager = new OctopusClutterGtk.ClutterGtkEventManager ({ window: this.applicationWindow })
	this.eventManager.connect ('key-event', Lang.bind(this, function (/*ClutterGtkEventManager*/em, /*uint*/keyval, /*string*/str, /*Gdk.ModifierType*/modifiers) {
		//Ctrl + f
		if (   (keyval == Gdk.KEY_f) //Clutter.f
			&& ((modifiers & Gdk.ModifierType.CONTROL_MASK) == Gdk.ModifierType.CONTROL_MASK)) {
			if (this.eventManager.fullscreen) {
				this.applicationWindow.unfullscreen ();
			} else {
				this.applicationWindow.fullscreen ();
			}
		}
		//F5 - Refresh
		else if (keyval == Gdk.KEY_F5) { //Clutter.KEY_F5
			if (this.medialibPanel.tracklistPanelFrame.get_n_children () > 0) {
				this.medialibPanel.tracklistPanel.tracklistPanelClose ();
			}
			this.medialibPanel.load ();
		}
		//Escape
		else if (keyval == Gdk.KEY_Escape) { //Clutter.KEY_Escape
			if (this.medialibPanel.tracklistPanelFrame.get_n_children () > 0) {
				this.medialibPanel.tracklistPanel.tracklistPanelClose ();
			} else if (this.medialibPanel.browsePanel.browsePanelSearchStack.visible) {
				this.medialibPanel.browsePanel.browsePanelSearchEntryClose ();
			} else if (this.medialibPanel.collectionPanel.collectionPanelSearchStack.visible) {
				this.medialibPanel.collectionPanel.collectionPanelSearchEntryClose ();
			} else if (this.eventManager.fullscreen) {
				this.applicationWindow.unfullscreen ();
			}
		}
		else {
			this.keyFocusActor.onKeyPressEvent (/*uint*/keyval, /*string*/str, /*Gdk.ModifierType*/modifiers);
		}
	}));
	//this.stage = applicationWindow.get_stage ();
	this.clutterEmbed = new GtkClutter.Embed ();
	this.applicationWindow.add (this.clutterEmbed);
	this.clutterEmbed.show ();
	this.stage = this.clutterEmbed.get_stage ();
	OctopusMx.MxTracklist.set_stage (this.stage);
	this.applicationWindow.set_title ("Octopus - Classic");
	this.applicationWindow.set_default_size (1024, 768);

	this.stageStack = new Mx.Stack ({ name: 'stage-background',
	                                  width: 1024,
	                                  height: 768 });
	this.stage.add_child (this.stageStack);

	this.controlPanelFrame = new Mx.Frame ({ x_align: Clutter.ActorAlign.FILL,
	                                         y_align: Clutter.ActorAlign.START });
	this.stageStack.add_child (this.controlPanelFrame);
	this.controlPanel = new ControlPanel.ControlPanel(this.controlPanelFrame);

	this.medialibPanelFrame = new Mx.Frame ({ x_align: Clutter.ActorAlign.FILL,
	                                          y_align: Clutter.ActorAlign.FILL });
	this.stageStack.add_child (this.medialibPanelFrame);
	this.medialibPanel = new MedialibPanel.MedialibPanel (this.medialibPanelFrame);
	this.keyFocusActor = this.medialibPanel.keyFocusActor;

	this.bottomPanelFrame = new Mx.Frame ({ x_align: Clutter.ActorAlign.FILL,
	                                        y_align: Clutter.ActorAlign.END });
	this.stageStack.add_child (this.bottomPanelFrame);
	this.bottomPanel = new BottomPanel.BottomPanel (this.bottomPanelFrame);

	this.playlistPanelFrame = new Mx.Frame ({ x_align: Clutter.ActorAlign.END,
	                                          y_align: Clutter.ActorAlign.FILL });
	this.stageStack.add_child (this.playlistPanelFrame);
	this.playlistPanel = new PlaylistPanel.PlaylistPanel (this.playlistPanelFrame);

	this.loadScreenFrame = new Mx.Frame ();
	this.stageStack.add_child (this.loadScreenFrame);
	this.loadScreen = new LoadScreen.LoadScreen(this.loadScreenFrame);

	this.stageOverlay = new Mx.Frame ({ name: 'stage-overlay' });
	this.stageStack.add_child (this.stageOverlay);

	this.medialibPanel.connect('load-finished', Lang.bind(this, function () {
		this.loadScreenFrame.visible = false;
	}));

	this.medialibPanel.load ();

	//connect to signals
	global.connect('message', Lang.bind(this, function (/*Octopus.Global*/obj, msg) {
		this.bottomPanel.setText (msg);
	}));
	this.controlPanel.connect('show-message-bar-text', Lang.bind(this, function (controlPanel, msg) {
		this.bottomPanel.setText (msg);
	}));
	this.medialibPanel.connect('show-message-bar-text', Lang.bind(this, function (medialibPanel, msg) {
		//print ("show-message-bar-text: " + msg);
		this.bottomPanel.setText (msg);
	}));
	this.playlistPanel.connect('show-message-bar-text', Lang.bind(this, function (playlistPanel, msg) {
		this.bottomPanel.setText (msg);
	}));
	this.bottomPanel.connect ('refresh-album-list', Lang.bind(this, function () {
		if (this.medialibPanel.tracklistPanelFrame.get_n_children () > 0) {
			this.medialibPanel.tracklistPanel.tracklistPanelClose ();
		}
		this.medialibPanel.load ();
	}));
	this.bottomPanel.connect ('toggle-fullscreen', Lang.bind(this, function () {
		this.stage.set_fullscreen (!this.stage.get_fullscreen ());
	}));
	this.bottomPanel.connect ('quit', Lang.bind(this, function () {
			this.applicationWindow.destroy ();
	}));
	this.playlistPanel.connect('grab-key-focus', Lang.bind(this, function () {
		this.keyFocusActor = this.playlistPanel;
	}));
	this.medialibPanel.connect('grab-key-focus', Lang.bind(this, function () {
		this.keyFocusActor = this.medialibPanel.keyFocusActor;
	}));

	this.stage.connect ('notify::allocation', Lang.bind(this, function () {
		this.stageStack.set_width (this.stage.width);
		this.stageStack.set_height (this.stage.height);
	}));

	this.config = new Octopus.Configure ();
	let window_width = 1024; //min. width
	let window_height = 768; //min. height
	if (this.config.has_key ("classic", "width")) {
		window_width =  this.config.get_integer("classic", "width");
	}
	if (this.config.has_key("classic", "height")) {
		window_height = this.config.get_integer("classic", "height");
	}
	if (window_width >= 1024 && window_height >= 768) {
		this.applicationWindow.resize(window_width, window_height);
	}

	this.applicationWindow.show_all();
	Gtk.main ();
}
